package edu.unc.comp410.colemangeer.data;
//TODO: maybe this should be a forced-singleton

import edu.unc.comp410.colemangeer.model.*;
import java.util.*;
import edu.unc.comp410.colemangeer.ui.*;

import java.io.FileNotFoundException;
import java.util.*;

public class DB_Cache2 { //TODO: maybe make this a singleton, not static
	private static Set<CatalogItem> cacheSet;
	//private static Hashtable cacheHT;
	private static boolean cacheExists = false; //this must be set if ANY records exist in the cache
	private static boolean cacheIsFull;  //this is set on the first hit
	private static Integer tableSize;
	//public static boolean 
	
	public DB_Cache2( Set<CatalogItem> theSet ){
		cacheSet = theSet;
		cacheExists = true;
		//maybe the settablesize call should go here (if we make it a sigleton)
	}
	
	public static void setTableSize( Integer theSize ){
		tableSize = theSize;
	}
	
	public static boolean isFull(){
		if( BrowseGeneralGUI.USE_EMULATOR ){ //Off the network
			//return false;  //TODO: be sure to switch this to true sometimes during testing
			return true;
		}
		else{ //USE_EMULATOR is false, so we must be on the UNC network
			return cacheSet.size()==tableSize;
		}
	}
	
	public static void clear(){
		cacheSet.clear();
		cacheExists = false;
	}
	
	public static Set<CatalogItem> getCacheSet(){
		return cacheSet;
	}
	
	//method meant purely for testing purposes - EMULATOR ONLY!!!!!!!!!!!!!
	/*public static Set<CatalogItem> getFullCacheSet_Test(){
		Set<CatalogItem> getFullSet = null;
		try{
			ProductGatewayEmulator PG = new ProductGatewayEmulator(); 
			getFullSet = PG.getAllProducts();  //run the "getAll" query
		}
		catch (FileNotFoundException x) {
			x.printStackTrace();
		}
		return getFullSet;
	}*/
	
	/*public Hashtable getCacheHT(){
		Hashtable result = cacheHT;
		
		
		return result;
	}*/
	
	public static boolean doesCacheExist(){
		return cacheExists;
	}
	
	public static Set<CatalogItem> getCacheByName( String theString ){
		Set<CatalogItem> result = new HashSet<CatalogItem>();
		//TODO: possibly handle errors here
		for( CatalogItem i : cacheSet ){
			CatalogItem theCat = i;
			//System.out.println("current catalogItem is: *" + theCat.getName() + "*");
			String currentString = theCat.getName();
			if( currentString.contains( theString ) ){
				
				result.add( theCat );
			}
		}
		return result;
	}
	
	//this will load all data at once, once and for all
	public static void loadCache(){
		
	}
	
	public void translateFromSetToHT(){
		
	}
	
	
}
